﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Savage.SecureData.SDObjectCreator
{
    class Program
    {
        static void Main(string[] args)
        {
            string columnTemplate = GetTemplateSql("Savage.SecureData.SDObjectCreator.SQL_Templates.ColumnTemplate.txt");
            string viewTemplate = GetTemplateSql("Savage.SecureData.SDObjectCreator.SQL_Templates.ViewTemplate.txt");
            string triggerTemplate = GetTemplateSql("Savage.SecureData.SDObjectCreator.SQL_Templates.TriggerTemplate.txt");

            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            Savage.DotNetLibrary.DatabaseLibrary.DatabaseManager dbm = new Savage.DotNetLibrary.DatabaseLibrary.DatabaseManager(connectionString);
                
            foreach (string table in args)
            {
                string[] sqlCommands = new string[] { BuildSqlFromTemplate(table, columnTemplate),
                    BuildSqlFromTemplate(table, viewTemplate), BuildSqlFromTemplate(table, triggerTemplate) };
                dbm.SqlBatch.ExecuteSqlBatchesInTransaction(sqlCommands);
            }
            Console.In.ReadLine();
        }

        static string BuildSqlFromTemplate(string tableName, string template)
        {
            string viewName = Regex.Replace(tableName, "^tbl_", "");
                
            string sql = template;

            //TODO: See if this is a create or alter
            sql = sql.Replace("[CREATE/ALTER_VIEW]", "CREATE");
            sql = sql.Replace("[CREATE/ALTER_TRIGGER]", "CREATE");

            sql = sql.Replace("[DATABASE_NAME]", "");
            sql = sql.Replace("[VIEW_NAME]", viewName);
            sql = sql.Replace("[TABLE_NAME]", tableName);
            sql = sql.Replace("[SCHEMA_NAME]", viewName.ToLowerInvariant());
            sql = sql.Replace("[SECURABLE_ID]", ConfigurationManager.AppSettings["SecurableIdColumnName"]);
            sql = sql.Replace("[SECURE_DATA_DATABASE_NAME]", ConfigurationManager.AppSettings["SecureDataDatabaseName"]);
            return sql;
        }

        static string GetTemplateSql(string resourceName)
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            
            using (System.IO.StreamReader reader = new System.IO.StreamReader(assembly.GetManifestResourceStream(resourceName)))
            {    
                return reader.ReadToEnd();
            }
        }

        static Stream GetEmbeddedFile(string assemblyName, string fileName)
        {
            try
            {
                System.Reflection.Assembly a = System.Reflection.Assembly.Load(assemblyName);
                Stream str = a.GetManifestResourceStream(assemblyName + "." + fileName);

                if (str == null)
                    throw new Exception("Could not locate embedded resource '" + fileName + "' in assembly '" + assemblyName + "'");
                return str;
            }
            catch (Exception e)
            {
                throw new Exception(assemblyName + ": " + e.Message);
            }
        }
    }
}
